package com.bupt.smart.ui.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bupt.smart.model.recAddress;
import com.bupt.smart.smart.R;
import com.bupt.smart.ui.activity.AddressActivity;
import com.bupt.smart.ui.activity.MarketActivity;
import com.bupt.smart.ui.widget.FlipperListView;

import java.io.Serializable;
import java.util.List;

/**
 * Created by WWL on 2016/5/31.
 */
public class AddressAdapter extends BaseAdapter {
    private List<recAddress> datas;
    private LayoutInflater layoutInflater;
    private Flag flag = new Flag();
    public boolean[] ishiden;
    int del_postion;
    private Context context;
    FlipperListView flipperListView;
    //构造函数
    public AddressAdapter(Context context,List<recAddress> datas,FlipperListView flipperListView){
        ishiden = new boolean[datas.size()];
        for(int i=0;i<ishiden.length;i++)
            ishiden[i] = true;
        this.context= context;
        layoutInflater = LayoutInflater.from(context);
        this.datas = datas;
        this.flipperListView = flipperListView;
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    public recAddress getSelectedAddr(){
        return datas.get(flag.index);
    }
    //准备绘制listview的每一行
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Zujian zujian;
        final ViewGroup parentfinal = parent;
        if(convertView==null){
            zujian = new Zujian();
            convertView = layoutInflater.inflate(R.layout.listview_addr,null);
            zujian.chosen = (ImageView) convertView.findViewById(R.id.choose);
            zujian.name = (TextView) convertView.findViewById(R.id.name);
            zujian.phonenumber = (TextView) convertView.findViewById(R.id.phonenumber);
            zujian.buildingnumber = (TextView) convertView.findViewById(R.id.buildingnumber);
            zujian.housenumber = (TextView) convertView.findViewById(R.id.housenumber);
            zujian.edit = (Button) convertView.findViewById(R.id.edit);
            zujian.delete = (Button)convertView.findViewById(R.id.delete);
            convertView.setTag(zujian);
        }
        else
        zujian = (Zujian) convertView.getTag();
        //编辑按钮，跳转AddaddressActivity 中修改地址
        zujian.edit.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Log.i("editposition", position + "");
                ((AddressActivity)context).address_edit(datas.get(position));

            }
        });
        //删除按钮
        zujian.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //将地址ID传递给address_del函数
                ((AddressActivity)context).delAddress(datas.get(position).getId());
                datas.remove(position);
                flipperListView.Address_Del(position);

            }
        });
        zujian.chosen.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Bitmap bitmap_choose = BitmapFactory.decodeResource(v.getResources(), R.mipmap.btn_icon_choose);
                Bitmap bitmap_chosen = BitmapFactory.decodeResource(v.getResources(), R.mipmap.btn_icon_chosen);

                if (flag.index == position) {

                    if (flag.flag == false) {
                        flag.settrue();
                        zujian.chosen.setImageBitmap(bitmap_chosen);
                    } else {
                        flag.setfalse();
                        zujian.chosen.setImageBitmap(bitmap_choose);
                    }
                } else if (flag.index == -1) {
                    flag.settrue();
                    zujian.chosen.setImageBitmap(bitmap_chosen);
                    flag.index = position;
                } else {
                    ((ImageView) parentfinal.getChildAt(flag.index).findViewById(R.id.choose)).setImageBitmap(bitmap_choose);
                    zujian.chosen.setImageBitmap(bitmap_chosen);
                    flag.index = position;
                    flag.settrue();
                }
                //选定地址，回传到AddressActivity
                if (flag.flag)
                    if (flag.index >= 0)
                        ((AddressActivity)context).getChoosenAddress(datas.get(flag.index));
                ((AddressActivity)context).changeState(flag.flag);


            }
        });
        zujian.name.setText(datas.get(position).getName());
        zujian.phonenumber.setText(datas.get(position).getPhonenumber());
        zujian.buildingnumber.setText(datas.get(position).getBuilding());
        zujian.housenumber.setText(datas.get(position).getHousenumber());

        return convertView;
    }


    public static class Zujian{
        public ImageView chosen;
        public TextView name;
        public TextView phonenumber;
        public TextView buildingnumber;
        public TextView housenumber;
        public Button edit;
        public Button delete;

    }
    public static class Flag{
        public boolean flag;
        public int index ;
        public Flag(){
            flag = false;
            index = -1;
        }
        public void setfalse(){
            flag = false;
        }
        public void settrue(){
            flag = true;
        }
    }
}





